<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
</head>

<body>
    <header class="aui-bar aui-bar-nav main-header">
        <a class="aui-pull-left aui-btn" tapmode onclick="closeWin();">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title" id="title">
            选择库位
        </div>
    </header>
    <div class="aui-bar aui-bar-nav sub-header"></div>
    <script id="workCellTemp" type="text/x-dot-template">

        {{~ it:value:index}}
        <li class="aui-list-item" onclick="setWorkCell('{{= it[index].workCellGid }}','{{= it[index].workCellCode }}','{{= it[index].workCellName }}');">
            <div class="aui-list-item-inner">
                <div class="aui-list-item-title">{{= it[index].workCellCode }}:{{= it[index].workCellName }}</div>
            </div>
        </li>
        {{~}}

    </script>
    <div class="aui-content aui-margin-b-15" style="margin-top:50px;">
        <div class="white_background">
            <ul class="aui-list aui-form-list">
                <li class="aui-list-item">
                    <div class="aui-list-item-inner">
                        <div class="aui-list-item-label">仓库</div>
                        <div class="aui-list-item-input">
                            <input type="text" placeholder="选择/扫描 仓库" id="workCenterCode" value=""
                                onclick="getIdSelector();" />
                        </div>
                        <i id="centerSelect" class="aui-input-addon aui-iconfont aui-icon-right"
                            onclick="goSelectWorkCenter()"></i>
                    </div>
                </li>
                <li class="aui-list-item">
                    <div class="aui-list-item-inner">
                        <div class="aui-list-item-label">库位</div>
                        <div class="aui-list-item-input">
                            <input type="text" placeholder="选择/扫描 库位" id="workCellCodeS" value="">
                        </div>
                    </div>
                </li>
            </ul>
            <div class="aui-btn aui-btn-info aui-btn-block" onclick="doSearch()">查询</div>
            <ul class="aui-list aui-list-in" id="workCellList">

            </ul>
        </div>
    </div>

</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/doT.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript">
    var workCenterGid = "";//仓库GID

    apiready = function () {
        workCenterGid = api.pageParam.workCenterGid;

        if (workCenterGid != undefined && workCenterGid != null && workCenterGid != "") {
            console.log('workCenterGid:' + workCenterGid)
            doSearchWorkCenterInfo();
            $("#workCenterCode").attr('disabled', true);
            //设置仓库选择点击事件无效
            document.getElementById("centerSelect").onclick = function () { };
            doSearch()
        } else {
            console.log('仓库可编辑');
            $("#workCenterCode").attr('disabled', false);
            getworkCellList();
        }



    };

    //查询仓库列表
    function getworkCellList() {

        //库内库间页面标识
        var flag = api.pageParam.flag;
        //取调入库位
        var relWorkCellGid = api.pageParam.relWorkCellGid;
        //取条码
        var tm = api.pageParam.tm;
        //取源目标标识（1源2目标）
        var workCellFlag = api.pageParam.workCellFlag;
        //取仓库GID  --入库出库业务用
        var workCenterGid = api.pageParam.workCenterGid;
        //操作类型--工单上料用
        var operateType = api.pageParam.operateType;
        //工单号--工单上料用
        var workOrderCode = api.pageParam.workOrderCode;
        //取库位编码  --库内转移业务用
        var kwcode = api.pageParam.kwcode;
        //取仓库类别
        var centerClass = api.pageParam.centerClass;

        var un = localStorage.getItem("un");
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getWorkCell";
        var data = {
            values: { ////以表单方式提交参数（JSON对象）
                flag: flag, //库内库间标识（out_kn库内  out_kj库间）
                relWorkCellGid: relWorkCellGid, //调入库位
                tm: tm,
                centerClass: centerClass,//仓库类别
                kwcode: kwcode,
                workCellFlag: workCellFlag,
                workCenterGid: workCenterGid, //仓库GID  入库出库用
                operateType: operateType, //操作类型，工单上料用
                workOrderCode: workOrderCode, //工单号，工单上料用
                workCellCode: "",
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                if (myobj.code == '0') {
                    var mydata = myobj.list;
                    var evaluation = doT.template($api.dom('#workCellTemp').innerHTML);
                    $api.dom('#workCellList').innerHTML = evaluation(mydata);
                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });
    }

    //选则返回函数
    function setWorkCell(workCellGid, workCellCode, workCellName) {
        api.closeWin();
        var flag = api.pageParam.flag;
        var name = api.pageParam.name;
        //入库
        if ('in_rk' == flag) {
            api.execScript({
                name: name,
                script: 'setWorkCell("' + workCellGid + '","' + workCellCode + '","' + workCellName + '")'
            });


            //库内-库间转移
        } else if ('out_kn' == flag || "out_kj") {
            var workCellFlag = api.pageParam.workCellFlag;
            api.execScript({
                name: name,
                script: 'setWorkCell("' + workCellGid + '","' + workCellCode + '","' + workCellName + '","' + workCellFlag + '")'
            });
        }
    }

    //库位编码查询
    function doSearch() {

        //取库位编码  --库内转移业务用
        var kwcode = api.pageParam.kwcode;
        var workCellCodeS = $("#workCellCodeS").val();
        var un = localStorage.getItem("un");
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getWorkCell";
        var data = {
            values: { ////以表单方式提交参数（JSON对象）
                flag: "search", //库内库间标识（out_kn库内  out_kj库间）
                relWorkCellGid: "", //调入库位
                tm: "",
                workCellFlag: "",
                kwcode: kwcode,
                workCenterGid: workCenterGid, //仓库GID  入库出库用
                workCellCode: workCellCodeS,
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                if (myobj.code == '0') {
                    var mydata = myobj.list;
                    var evaluation = doT.template($api.dom('#workCellTemp').innerHTML);
                    $api.dom('#workCellList').innerHTML = evaluation(mydata);
                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });
    }

    //返回按钮
    function closeWin() {
        api.closeWin({});
    }

    // 获取光标所在组件的id
    function getIdSelector() {
        var e = event || window.event;
        var el = e.srcElement || e.target;
        idSelector = el.id;
        $("#" + el.id).select();
    }

    //选择仓库
    function goSelectWorkCenter() {
        api.openWin({
            name: 'uwmSelectWorkCenter',
            url: 'uwmSelectWorkCenter.html',
            pageParam: {
                flag: "in",
                name: "uwmSelectWorkCell",
                mrlCode: "",
                lotCode: "",
                qty: ""
            }
        });
    }

    //仓库选择后回调函数
    function setWorkCenter(id, code, name) {
        workCenterGid = id;
        //$("#workCenterCode").attr("value",code+":"+name);
        console.log('仓库代码：' + code);
        document.getElementById('workCenterCode').value = code + ":" + name;
        $("#workCellCode").focus();
        idSelector = "workCellCode";
    }


    //根据库区ID查询库区数据
    function doSearchWorkCenterInfo() {

        //取库位编码  --库内转移业务用
        var un = localStorage.getItem("un");
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getWorkCenterInfo";
        var data = {
            values: { ////以表单方式提交参数（JSON对象）
                workCenterGid: workCenterGid, //仓库GID
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                if (myobj.code == '0') {
                    var mydata = myobj.list;
                    console.log('mydata:' + JSON.stringify(mydata));
                    if (mydata != undefined && mydata != null && mydata != "") {
                        document.getElementById('workCenterCode').value = mydata.code + ":" + mydata.name;
                    }

                    // var evaluation = doT.template($api.dom('#workCellTemp').innerHTML);
                    // $api.dom('#workCellList').innerHTML = evaluation(mydata);
                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });
    }



</script>

</html>